<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <div v-if="isDeleteShow">
        <!-- 点击它删除所有的搜索历史 -->
        <span @click="$emit('clear-search-history')">全部删除</span>
        &nbsp;&nbsp;
        <span @click="isDeleteShow=false">完成</span>
      </div>
      <van-icon name="delete" @click="isDeleteShow=true" />
    </van-cell>
    <!-- 点击谁就删除它 onSearchItemClick()-->
    <van-cell :title="item" v-for="(item,index) in searchHistories"
      :key="index" @click="onSearchItemClick(item,index)">
      <van-icon name="close" v-show="isDeleteShow" />
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  components: {},
  props: {
    searchHistories: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      // 定义当前是否是删除状态
      isDeleteShow: false
    }
  },
  computed: {},
  watch: {},
  created () { },
  mounted () { },
  methods: {
    // 单项删除，点住哪个就删除它
    onSearchItemClick (item, index) {
      if (this.searchHistories) { // 有就执行删除
        this.searchHistories.splice(index, 1)
      } else {
        // 不删除就搜索
        this.$emit('search', item)
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
